package com.qiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiku.entity.LoginUser;
import com.qiku.entity.User;
import com.qiku.enums.AppHttpCodeEnum;
import com.qiku.exception.SystemException;
import com.qiku.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public LoginUser loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,username);
        User user = userMapper.selectOne(queryWrapper);
        //判断用户是否查到  若没查到    抛异常
        if (Objects.isNull(user)){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_EXIST);
        }
        //返回用户信息
        return new LoginUser(user);
    }
}
